//
//  AddContentController.m
//  万物记
//
//  Created by Ajen on 2019/6/16.
//  Copyright © 2019 Ajen. All rights reserved.
//

#import "AddContentController.h"
#import "ChooseColorView.h"
#import "TouchMoveSingleView.h"
#import "AddLeftRespondView.h"
#import "AddRightRespondView.h"
#import "AllScreenEditContentController.h"
#import "BRPickerView.h"
#import "KeyboardWordsToolView.h"
#import "SetLocalWordsController.h"
#import "BRStringPickerView.h"
#import "DYAdAlertView.h"
#import "DYAdModel.h"

@interface AddContentController ()<UITextFieldDelegate,UITextViewDelegate,DYAdAlertDelegate>{
    ChooseColorView * chooseColorView;
    NSArray * choosedColorArray;
    AddLeftRespondView * leftRespondView;
    AddRightRespondView * rightRespondView;
    UIImpactFeedbackGenerator *feedBackGenertor;
    LocalJsonModelContentslist * addItemModel;
    BOOL cannotSetTabIndex;
    CGFloat keyboardHeight;
    UIButton *wordsBtn;
    UIBarButtonItem * rightItem;
}
@property (weak, nonatomic) IBOutlet UILabel *fitstTitleLabel;
@property (weak, nonatomic) IBOutlet UITextField *titleTextField;
@property (weak, nonatomic) IBOutlet UITextView *contentTextView;
@property (weak, nonatomic) IBOutlet UIButton *allScreenEditButton;
@property(nonatomic,strong)UIToolbar * myToolbar;

@property(nonatomic)TouchMoveSingleView * touchView;
@property(nonatomic)KeyboardWordsToolView * wordsToolView;
@property(nonatomic)UIVisualEffectView *effectView;

@end

@implementation AddContentController

- (void)viewDidLoad {
    [super viewDidLoad];
    feedBackGenertor = [[UIImpactFeedbackGenerator alloc] initWithStyle:UIImpactFeedbackStyleMedium];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardFrameChange:) name:UIKeyboardWillChangeFrameNotification object:nil];
    
    
    
    addItemModel = [LocalJsonModelContentslist new];
    if(self.editItemModel){//当前控制器功能是修改
        addItemModel = self.editItemModel;
        AJLog(@"%@",self.editItemModel.mj_keyValues);
        rightItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(rightItemDown)];
        rightItem.tintColor = [UIColor colorWithAjenHexString:@"#7a7374"];
        self.navigationItem.rightBarButtonItem= rightItem;
    }

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    UILabel * titleLabel = [UILabel new];
    titleLabel.aj_Text(self.editItemModel ? @"编辑" : @"新增内容").aj_textColorHexStr(@"#2d2e36").aj_addToView(self.view);
    titleLabel.font = SongKeFont(23);
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view).offset(20);
        make.top.equalTo(self.view).offset(STATUS_BAR_HEIGHT);
        make.height.equalTo(@44);
    }];
    self.navigationItem.titleView = titleLabel;

    WeakObj(self);
    [self.view addTapBlock:^(id obj) {
        [weak_self.view endEditing:YES];
    }];

    self.titleTextField.delegate = self;
    self.contentTextView.delegate = self;
    self.titleTextField.inputAccessoryView = self.myToolbar;
    self.contentTextView.inputAccessoryView = self.myToolbar;

    self.titleTextField.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 5, 0)];
    self.titleTextField.leftViewMode = UITextFieldViewModeAlways;


    [self.view layoutIfNeeded];
    [self.allScreenEditButton setEnlarge:20];
    
    chooseColorView = [[ChooseColorView alloc] initWithFrame:CGRectMake(0, self.contentTextView.Bottom, SCREEN_WIDTH, 200)];
    chooseColorView.userInteractionEnabled = YES;
    [self.view addSubview:chooseColorView];
    
    chooseColorView.actionBlock = ^(NSArray * _Nonnull colorArray) {
        AJLog(@"%@",colorArray);
        choosedColorArray = colorArray;
    };

    

    CGFloat width = SCREEN_WIDTH * 0.4;
    leftRespondView = [[AddLeftRespondView alloc] initWithFrame:CGRectMake(-(width/2), SCREEN_HEIGHT - STATUS_BAR_NAV_HEIGHT - width, width, width)];
    leftRespondView.backgroundColor = [UIColor colorWithAjenHex:0x4a4035 alpha:0.8];
    leftRespondView.aj_cornerRadius(width/2);
    [self.view addSubview:leftRespondView];

    rightRespondView = [[AddRightRespondView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-(width/2), SCREEN_HEIGHT - STATUS_BAR_NAV_HEIGHT - width, width, width)];
    rightRespondView.backgroundColor = [UIColor colorWithAjenHex:0x29453 alpha:0.8];
    rightRespondView.aj_cornerRadius(width/2);
    [self.view addSubview:rightRespondView];
    
    leftRespondView.hidden = YES;
    rightRespondView.hidden = YES;
    

    self.touchView = [[TouchMoveSingleView alloc] initWithFrame:CGRectMake(10, 10, 10, 10) superView:self.view];
    [self.view addSubview:self.touchView];
    [self.touchView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftRespondView);
        make.centerX.equalTo(self.view);
        make.width.height.mas_equalTo(@50);
    }];


    self.touchView.iconImage = [UIImage imageNamed:@"lf"];
    self.touchView.stayPoint = CGPointMake(SCREEN_WIDTH / 2, leftRespondView.CenterY);
    
    UIButton * deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    deleteButton.aj_buttonImgName(@"删除").aj_addTargetSelector(self,@selector(deleteButtonDown))/*.aj_addToView(self.view)*/;
//    [deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.touchView);
//        make.left.equalTo(self.view).offset(30);
//        make.width.height.equalTo(@(30));
//    }];
//    [self.view layoutIfNeeded];
//    [deleteButton setEnlarge:15];
    
    UIButton * moveToOtherTabButton = [UIButton buttonWithType:UIButtonTypeCustom];
    moveToOtherTabButton.aj_buttonImgName(@"moveToOtherTab").aj_addTargetSelector(self,@selector(moveToOtherTabButtonDown))/*.aj_addToView(self.view)*/;
//    [moveToOtherTabButton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(self.touchView);
//        make.right.equalTo(self.view).offset(-27.5);
//        make.width.height.equalTo(@(35));
//    }];
//    [self.view layoutIfNeeded];
//    [moveToOtherTabButton setEnlarge:15];
    
    //


    //悬浮球松手
    self.touchView.moveEndBlock = ^(CGPoint endPoint) {
        self.view.userInteractionEnabled = YES;
        leftRespondView.hidden = YES;
        rightRespondView.hidden = YES;
        deleteButton.hidden = NO;
        moveToOtherTabButton.hidden = NO;

        if(CGRectContainsPoint(leftRespondView.frame, endPoint)){
            [self.navigationController popViewControllerAnimated:YES];
        }
        
        if(CGRectContainsPoint(rightRespondView.frame, endPoint)){

            AJLog(@"%@",self.titleTextField.text);
            if(self.titleTextField.text.length < 1){
                [weak_self showMsg:@"您还没起名字哦~"];
                return ;
            }
            if (self.titleTextField.text.length > 50) {
                [self.titleTextField resignFirstResponder];
                [self showMsg:@"主人~ 名字太长了,超过50字了~"];
                return;
            }
            if (self.contentTextView.text.length > 5000) {
                [self.contentTextView resignFirstResponder];
                [self showMsg:@"主人~ 内容太长了,超过5000字了~"];
                return;
            }
            //保存并判断是否可以向下执行
            if(![self saveCurrentContents]){
                return ;
            }
            
            [deleteButton removeFromSuperview];
            [moveToOtherTabButton removeFromSuperview];
            
            rightRespondView.hidden = NO;
            [rightRespondView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
            [kWindow addSubview:rightRespondView];
//            [self.view bringSubviewToFront:rightRespondView];
            CGFloat width = SCREEN_HEIGHT * 3;
            [rightRespondView mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.height.equalTo(@(width));
                make.centerY.equalTo(kWindow.mas_bottom);
                make.centerX.equalTo(kWindow.mas_right);
            }];

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.navigationController popViewControllerAnimated:NO];
            });

            UIImageView * successImg = [UIImageView new];
            successImg.aj_imgName(@"成功-表情");

            UILabel * titleLabel = [UILabel new];
            titleLabel.aj_Text(@"万物记").aj_numberOfLines(0).aj_textColorHexStr(@"#ffffff");
            titleLabel.font = SongKeFont(45);

            UILabel * titleLabel2 = [UILabel new];
            titleLabel2.aj_Text(@"记住你的Anything~").aj_textColorHexStr(@"#ffffff");
            titleLabel2.font = SongKeFont(16);

            UILabel * saveSuccessedLabel = [UILabel new];
            saveSuccessedLabel.aj_Text(@"保存成功").aj_font([UIFont systemFontOfSize:18 weight:UIFontWeightMedium]).aj_textColorHexStr(@"#ffffff");



            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [kWindow addSubview:successImg];
                [successImg mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.width.height.equalTo(@(kWindow.Width * 0.35));
                    make.center.equalTo(kWindow);
                }];

                [kWindow addSubview:titleLabel];
                [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.equalTo(successImg.mas_bottom).offset(30);
                    make.centerX.equalTo(successImg);
                }];

                [kWindow addSubview:titleLabel2];
                [titleLabel2 mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(titleLabel);
                    make.top.equalTo(titleLabel.mas_bottom).offset(15);
                }];

                [kWindow addSubview:saveSuccessedLabel];
                [saveSuccessedLabel mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(titleLabel);
                    make.centerY.equalTo(kWindow.mas_bottom).offset(-(SCREEN_WIDTH * 0.2));
                }];

                [UIView animateWithDuration:0.3 animations:^{
                    successImg.alpha = 1;
                    titleLabel.alpha = 1;
                    titleLabel2.alpha = 1;
                    saveSuccessedLabel.alpha = 1;
                }];

            });

            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.6 animations:^{
                    successImg.alpha = 0;
                    titleLabel.alpha = 0;
                    titleLabel2.alpha = 0;
                    saveSuccessedLabel.alpha = 0;
                }];
            });



            [UIView animateWithDuration:1 animations:^{
                [kWindow layoutIfNeeded];
                rightRespondView.backgroundColor = [UIColor colorWithAjenHex:0x29453 alpha:1];
                rightRespondView.aj_cornerRadius(width/2);
            } completion:^(BOOL finished) {
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.05 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

                    [UIView animateWithDuration:1 animations:^{
                        rightRespondView.backgroundColor = [UIColor colorWithAjenHex:0x29453 alpha:3];
                        rightRespondView.frame = CGRectMake(-width/2, -width/2, 2, 2);
                        rightRespondView.aj_cornerRadius(1);
                    } completion:^(BOOL finished) {
                        [rightRespondView removeFromSuperview];
                        [successImg removeFromSuperview];
                        [titleLabel removeFromSuperview];
                        [titleLabel2 removeFromSuperview];
                    }];
                });

            }];
        }
    };

    self.touchView.moveChangeBlock = ^(CGPoint changePoint) {
        self.view.userInteractionEnabled = NO;
        deleteButton.hidden = YES;
        moveToOtherTabButton.hidden = YES;
        if(leftRespondView.hidden){
            leftRespondView.hidden = NO;
            leftRespondView.alpha = 0 ;
            [UIView animateWithDuration:0.15 animations:^{
                leftRespondView.alpha = 1 ;
            }];
        }
        if(rightRespondView.hidden){
            rightRespondView.hidden = NO;
            rightRespondView.alpha = 0 ;
            [UIView animateWithDuration:0.15 animations:^{
                rightRespondView.alpha = 1 ;
            }];
        }
        if(CGRectContainsPoint(leftRespondView.frame, changePoint)){
            [self endMoveToRightView];
            [self startMoveToLeftView];
        }else if(CGRectContainsPoint(rightRespondView.frame, changePoint)){
            [self endMoveToLeftView];
            [self startMoveToRightView];
        }else{
            [self endMoveToLeftView];
            [self endMoveToRightView];
        }
    };
    
    
    if(self.editItemModel){//当前控制器功能是修改,给视图赋值
        self.titleTextField.text = self.editItemModel.title;
        self.contentTextView.text = self.editItemModel.content;
        if(self.editItemModel.colorsHex.count){
            chooseColorView.localColorArray = self.editItemModel.colorsHex;
        }
    }else{
        [deleteButton removeFromSuperview];
        [moveToOtherTabButton removeFromSuperview];
    }
    
    ///展示教程
    [self showExplain];
}


//保存当前内容
-(BOOL)saveCurrentContents{
    addItemModel.title = self.titleTextField.text;
    addItemModel.content = self.contentTextView.text;
    addItemModel.address = @"";
    addItemModel.time = @"";
    addItemModel.lat = @"";
    addItemModel.lng = @"";
    if(choosedColorArray.count){
        addItemModel.colorsHex = choosedColorArray;
    }else if (addItemModel.colorsHex.count){
        addItemModel.colorsHex = addItemModel.colorsHex;
    }else{
        addItemModel.colorsHex = choosedColorArray;
    }
    
    
    if(self.editItemModel){//修改
        [[LocalModel shared] editContent:addItemModel belongTabIndex:self.tabIndex andEditContentListIndex:self.editItemIndex];
    }else{//新增
        if(self.insertItemIndex){//插入
            [[LocalModel shared] addNewContent:addItemModel inTabIndex:self.tabIndex andInContentListIndex:self.insertItemIndex];
        }else{//普通新增
            [[LocalModel shared] addNewContent:addItemModel inTabIndex:self.tabIndex andInContentListIndex:0];
        }
        
    }
    
    
    if(self.successBlock){
        self.successBlock();
    }
    return YES;
}



-(void)startMoveToLeftView{
    if(!leftRespondView.remind){
        leftRespondView.remind = YES;
        [feedBackGenertor impactOccurred];//震动反馈
    }
}
-(void)endMoveToLeftView{
    leftRespondView.remind = NO;
}

-(void)startMoveToRightView{
    if(!rightRespondView.remind){
        rightRespondView.remind = YES;
        [feedBackGenertor impactOccurred];//震动反馈
    }
}
-(void)endMoveToRightView{
    rightRespondView.remind = NO;
}


-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.titleTextField resignFirstResponder];
    [self.contentTextView becomeFirstResponder];
    return YES;
}

- (IBAction)EditChanged:(id)sender {
    
    if (self.titleTextField.text.length > 50) {
        [self.titleTextField resignFirstResponder];
        [self showMsg:@"主人~ 太长了,已经50字了~"];
        self.titleTextField.text = [self.titleTextField.text substringToIndex:50];
    }
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    AJLog(@"%@",textView.text);
    AJLog(@"%@",text);
    return YES;
}

- (void)textViewDidChange:(UITextView *)textView{
    AJLog(@"%@",textView.text);
    if (textView.markedTextRange == nil && textView.text.length > 5000) {
        [self.contentTextView resignFirstResponder];
        [self showMsg:@"主人~ 太长了,已经5000字了~"];
        textView.text = [textView.text substringToIndex:5000];
    }
}
- (IBAction)allScreenEditButtonDown:(id)sender {
    AllScreenEditContentController * allScreenEditController = [AllScreenEditContentController new];
    allScreenEditController.editString = self.contentTextView.text;
    allScreenEditController.endEditBlock = ^(NSString * _Nonnull string) {
        self.contentTextView.text = string;
    };
    [self.navigationController pushViewController:allScreenEditController animated:YES];
}


-(void)deleteButtonDown{
    if(self.deleteBlock){
        self.deleteBlock(self.editItemIndex);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)moveToOtherTabButtonDown{
    NSMutableArray * titleMuArray = [NSMutableArray arrayWithCapacity:0];
    [titleMuArray addObjectsFromArray:[LocalModel shared].titlesArray];
    [titleMuArray removeObjectAtIndex:self.tabIndex];
    [BRStringPickerView showStringPickerWithTitle:@"请选择要移动到的栏目" dataSource:titleMuArray defaultSelValue:nil isAutoSelect:NO themeColor:nil resultBlock:^(id selectValue) {
        NSLog(@"%@",selectValue);
        NSInteger toTabIndex = -1;//目标栏目下标
        for(int i = 0 ; i < [LocalModel shared].titlesArray.count ; i++){
            if([selectValue isEqualToString:[LocalModel shared].titlesArray[i]]){
                toTabIndex = i;
            }
        }
        if(toTabIndex >= 0){
            addItemModel.title = self.titleTextField.text;
            addItemModel.content = self.contentTextView.text;
            addItemModel.address = @"";
            addItemModel.time = @"";
            addItemModel.lat = @"";
            addItemModel.lng = @"";
            if(choosedColorArray.count){
                addItemModel.colorsHex = choosedColorArray;
            }else if (addItemModel.colorsHex.count){
                addItemModel.colorsHex = addItemModel.colorsHex;
            }else{
                addItemModel.colorsHex = choosedColorArray;
            }
            //将当前的item增加到目标栏目
            [[LocalModel shared] addNewContent:addItemModel inTabIndex:toTabIndex andInContentListIndex:0];
            
            //将当前的item从原有的栏目中删除
            [[LocalModel shared] deleteContentWithTabIndex:self.tabIndex andEditContentListIndex:self.editItemIndex];
            
            //通知首页刷新
            [[NSNotificationCenter defaultCenter] postNotificationName:@"REFRESH_HOME_TAB" object:nil userInfo:@{@"isEdit":@"YES"}];
            if(self.moveToOtherTabBlock){
                self.moveToOtherTabBlock();
            }
            [self.navigationController popViewControllerAnimated:YES];
        }
    } cancelBlock:^{
        NSLog(@"点击了背景视图或取消按钮");
    }];
}

- (UIToolbar *)myToolbar
{
    if (_myToolbar == nil) {
        CGRect tempFrame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 44);
        _myToolbar = [[UIToolbar alloc] initWithFrame:tempFrame];
        
        
        UIButton *clearBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [clearBtn setTitle:@"清空      " forState:UIControlStateNormal];
        clearBtn.aj_buttonFont([UIFont systemFontOfSize:14 weight:UIFontWeightMedium]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]);
        [clearBtn setFrame:CGRectMake(0, 0, 30, 30)];
        [clearBtn addTarget:self action:@selector(clearButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        UIBarButtonItem *clearItem = [[UIBarButtonItem alloc]initWithCustomView:clearBtn];
        
        wordsBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [wordsBtn setTitle:@"   词条   " forState:UIControlStateNormal];
        wordsBtn.aj_buttonFont([UIFont systemFontOfSize:13 weight:UIFontWeightMedium]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]);
        [wordsBtn setFrame:CGRectMake(0, 0, 30, 30)];
        wordsBtn.tag = 100;
        [wordsBtn addTarget:self action:@selector(wordsButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        UIBarButtonItem *wordsItem = [[UIBarButtonItem alloc]initWithCustomView:wordsBtn];
        
        UIButton *timeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [timeBtn setTitle:@"   插入时间   " forState:UIControlStateNormal];
        timeBtn.aj_buttonFont([UIFont systemFontOfSize:13 weight:UIFontWeightMedium]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]);
        [timeBtn setFrame:CGRectMake(0, 0, 30, 30)];
        [timeBtn addTarget:self action:@selector(timeButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        UIBarButtonItem *timeItem = [[UIBarButtonItem alloc]initWithCustomView:timeBtn];
        
        UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [copyBtn setTitle:@"   复制全部   " forState:UIControlStateNormal];
        copyBtn.aj_buttonFont([UIFont systemFontOfSize:13 weight:UIFontWeightMedium]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]);
        [copyBtn setFrame:CGRectMake(0, 0, 30, 30)];
        [copyBtn addTarget:self action:@selector(copyButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        UIBarButtonItem *copyItem = [[UIBarButtonItem alloc]initWithCustomView:copyBtn];
        
        UIButton *pasteBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [pasteBtn setTitle:@"   粘贴   " forState:UIControlStateNormal];
        pasteBtn.aj_buttonFont([UIFont systemFontOfSize:13 weight:UIFontWeightMedium]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:0.5]);
        [pasteBtn setFrame:CGRectMake(0, 0, 30, 30)];
        [pasteBtn addTarget:self action:@selector(pasteButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        UIBarButtonItem *pasteItem = [[UIBarButtonItem alloc]initWithCustomView:pasteBtn];
        
        
        UIButton *closeBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [closeBtn setTitle:@"       完成" forState:UIControlStateNormal];
        closeBtn.aj_buttonFont([UIFont systemFontOfSize:15 weight:UIFontWeightBold]).aj_buttonTintColor([UIColor colorWithAjenHex:0x7A7374 alpha:1]);
        [closeBtn setFrame:CGRectMake(0, 0, 30, 30)];
        [closeBtn addTarget:self action:@selector(closeButtonDown) forControlEvents:UIControlEventTouchUpInside ];
        UIBarButtonItem *closeItem = [[UIBarButtonItem alloc]initWithCustomView:closeBtn];
        
        _myToolbar.items = @[clearItem,wordsItem,timeItem,copyItem,pasteItem,closeItem];
    }
    
    return _myToolbar;
}

-(void)clearButtonDown{
    if(self.titleTextField.isFirstResponder){
        if(!self.titleTextField.text.length){
            return;
        }
    }
    if(self.contentTextView.isFirstResponder){
        if(!self.contentTextView.text.length){
            return;
        }
    }
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"是否清空当前输入框内所有内容?" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"清空" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        if(self.titleTextField.isFirstResponder){
            self.titleTextField.text = @"";
        }
        if(self.contentTextView.isFirstResponder){
            self.contentTextView.text = @"";
        }
    }];
    [alertController addAction:cancelAction];
    [alertController addAction:okAction];
    [self presentViewController:alertController animated:YES completion:nil];
    
}

-(void)wordsButtonDown{
    if(wordsBtn.tag == 100){
        wordsBtn.tag = 200;
        [wordsBtn setTitle:@"   收回   " forState:UIControlStateNormal];
        self.effectView.frame = CGRectMake(0, self.view.Height - keyboardHeight - 44, SCREEN_WIDTH, 44);
        [self.view addSubview:self.effectView];
        self.wordsToolView.frame = CGRectMake(0, self.view.Height - keyboardHeight - 44, SCREEN_WIDTH, 44);
        [self.view addSubview:self.wordsToolView];
        WeakObj(self);
        self.wordsToolView.singleWordBlock = ^(NSString * _Nonnull wordString) {
            
            if(weak_self.titleTextField.isFirstResponder){
                NSInteger positionIndex = weak_self.titleTextField.selectedRange.location;
                NSMutableString * muString = [NSMutableString stringWithCapacity:0];
                [muString appendString:weak_self.titleTextField.text];
                [muString insertString:wordString atIndex:positionIndex];
                weak_self.titleTextField.text = muString;
                [weak_self.titleTextField setSelectedRange:NSMakeRange(positionIndex + wordString.length, 0)];
            }
            if(weak_self.contentTextView.isFirstResponder){
                NSInteger positionIndex = weak_self.contentTextView.selectedRange.location;
                NSMutableString * muString = [NSMutableString stringWithCapacity:0];
                [muString appendString:weak_self.contentTextView.text];
                [muString insertString:wordString atIndex:positionIndex];
                weak_self.contentTextView.text = muString;
                [weak_self.contentTextView setSelectedRange:NSMakeRange(positionIndex + wordString.length, 0)];
            }
        };
        self.wordsToolView.setWordsBlock = ^{
            wordsBtn.tag = 100;
            [wordsBtn setTitle:@"   词条   " forState:UIControlStateNormal];
            [self.effectView removeFromSuperview];
            [self.wordsToolView removeFromSuperview];
            [weak_self.titleTextField resignFirstResponder];
            [weak_self.contentTextView resignFirstResponder];
            SetLocalWordsController * setWordsController = [SetLocalWordsController new];
            [weak_self.navigationController pushViewController:setWordsController animated:YES];
        };
    }else{
        wordsBtn.tag = 100;
        [wordsBtn setTitle:@"   词条   " forState:UIControlStateNormal];
        [self.effectView removeFromSuperview];
        [self.wordsToolView removeFromSuperview];
    }
    
}

-(void)timeButtonDown{
    if(self.titleTextField.isFirstResponder){
        NSInteger positionIndex = self.titleTextField.selectedRange.location;
        [self closeButtonDown];
        [BRDatePickerView showDatePickerWithTitle:@"" dateType:BRDatePickerModeYMDHM defaultSelValue:nil minDate:nil maxDate:nil isAutoSelect:YES themeColor:nil resultBlock:^(NSString *selectValue) {
            [self.titleTextField becomeFirstResponder];
            NSMutableString * muString = [NSMutableString stringWithCapacity:0];
            [muString appendString:self.titleTextField.text];
            [muString insertString:selectValue atIndex:positionIndex];
            self.titleTextField.text = muString;
            [self.titleTextField setSelectedRange:NSMakeRange(positionIndex + selectValue.length, 0)];
        } cancelBlock:^{
            [self.titleTextField becomeFirstResponder];
            [self.titleTextField setSelectedRange:NSMakeRange(positionIndex, 0)];
        }];
    }
    if(self.contentTextView.isFirstResponder){
        NSInteger positionIndex = self.contentTextView.selectedRange.location;
        [self closeButtonDown];
        [BRDatePickerView showDatePickerWithTitle:@"" dateType:BRDatePickerModeYMDHM defaultSelValue:nil minDate:nil maxDate:nil isAutoSelect:NO themeColor:nil resultBlock:^(NSString *selectValue) {
            [self.contentTextView becomeFirstResponder];
            NSMutableString * muString = [NSMutableString stringWithCapacity:0];
            [muString appendString:self.contentTextView.text];
            [muString insertString:selectValue atIndex:positionIndex];
            self.contentTextView.text = muString;
            [self.contentTextView setSelectedRange:NSMakeRange(positionIndex + selectValue.length, 0)];
        } cancelBlock:^{
            [self.contentTextView becomeFirstResponder];
            [self.contentTextView setSelectedRange:NSMakeRange(positionIndex, 0)];
        }];
    }
}
-(void)copyButtonDown{
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    
    if(self.titleTextField.isFirstResponder){
        [pasteboard setString:self.titleTextField.text];
    }
    if(self.contentTextView.isFirstResponder){
        [pasteboard setString:self.contentTextView.text];
    }
}
-(void)pasteButtonDown{
    NSString *content = [[UIPasteboard generalPasteboard] string];

    if(self.titleTextField.isFirstResponder){
        self.titleTextField.text = [NSString stringWithFormat:@"%@%@",self.titleTextField.text,content];
    }
    if(self.contentTextView.isFirstResponder){
        self.contentTextView.text = [NSString stringWithFormat:@"%@%@",self.contentTextView.text,content];
    }
}
-(void)closeButtonDown{
    [self.titleTextField resignFirstResponder];
    [self.contentTextView resignFirstResponder];
}











-(UIVisualEffectView *)effectView{
    if(!_effectView){
        UIBlurEffect *effect = [UIBlurEffect effectWithStyle:0];
        _effectView = [[UIVisualEffectView alloc] initWithEffect:effect];
    }
    return _effectView;
    
}

-(KeyboardWordsToolView *)wordsToolView{
    if(!_wordsToolView){
        _wordsToolView = [KeyboardWordsToolView new];
    }
    return _wordsToolView;
}

- (void)keyboardFrameChange:(NSNotification *)notifi{
    
    NSDictionary *userInfo = notifi.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    void(^animations)(void) = ^{
        [self willShowKeyboardFromFrame:beginFrame toFrame:endFrame];
    };
    
    void(^completion)(BOOL) = ^(BOOL finished){
        
    };
    
    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:completion];
    
}

- (void)willShowKeyboardFromFrame:(CGRect)beginFrame toFrame:(CGRect)toFrame
{
    if (toFrame.origin.y  == [[UIScreen mainScreen] bounds].size.height){//键盘收起
        wordsBtn.tag = 100;
        [wordsBtn setTitle:@"   词条   " forState:UIControlStateNormal];
        [self.effectView removeFromSuperview];
        [self.wordsToolView removeFromSuperview];
    }else{ //键盘升起
        keyboardHeight = toFrame.size.height;
        
    }
}


-(void)rightItemDown{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"移动到其他栏目" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self moveToOtherTabButtonDown];
    }];
    [alert addAction:cameraAction];
    UIAlertAction *libraryAction = [UIAlertAction actionWithTitle:@"删除本条内容" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [self deleteButtonDown];
    }];
    [alert addAction:libraryAction];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    [alert addAction:cancelAction];
    [self presentViewController:alert animated:YES completion:nil];
}


///展示教程
-(void)showExplain{
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    if(![[userDefault objectForKey:@"IS_SHOW_ADDCONTENT_EXPLAIN"] isEqualToString:@"YES"]){
        NSArray * imgNameArr = @[@"工具栏介绍.png",@"词条使用.png",@"新增内容.png"];
        
        NSMutableArray *arr = [[NSMutableArray alloc]initWithCapacity:0];
        for (int i = 0; i < imgNameArr.count; i++) {
            DYAdModel *adModel  = [[DYAdModel alloc]init];
            adModel.imgStr      = imgNameArr[i];
            adModel.linkUrl     = @"";
            [arr addObject:adModel];
        }
        [self showGaussianBlur];
        [kWindow addSubview:[DYAdAlertView  showInView:kWindow theDelegate:self theADInfo:arr placeHolderImage:@"工具栏介绍.png"]];
    }
}
-(void)closeDown{
    [self hideGaussianBlur];
    NSUserDefaults * userDefault = [NSUserDefaults standardUserDefaults];
    [userDefault setObject:@"YES" forKey:@"IS_SHOW_ADDCONTENT_EXPLAIN"];
}

@end
